package com.liwalo.utils;

import java.io.IOException;
import java.math.BigInteger;
import java.security.SecureRandom;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;

import com.liwalo.entities.Usuario;

public class URLUtils {

	public static void sendConfirmationEmail(Usuario u, String url) throws IOException, MessagingException{
        MailSenderUtil mailSender = MailSenderUtil.getInstance();
        mailSender.sendConfirmationEmail(u.getEmail(), url, u.getNombre()+" "+u.getApellidos());
    }
    
    public static String generateConfirmationToken(){
        SecureRandom random = new SecureRandom();
        String code = new BigInteger(130, random).toString(32);
        return code;
    }
    
    public static String getConfirmURL(String token, HttpServletRequest request){
        StringBuilder url = new StringBuilder();
        
        int port = request.getServerPort();
        if (port < 0) {
            port = 80; // Work around java.net.URL bug
        }
        String scheme = request.getScheme();
        url.append(scheme);
        url.append("://");
        url.append(request.getServerName());
        if ((scheme.equals("http") && (port != 80)) || (scheme.equals("https") && (port != 443))) {
            url.append(':');
            url.append(port);
        }
        url.append(request.getContextPath());
        
        return url.toString();
    }
    
    public static String getPaypalURL(HttpServletRequest request){
        StringBuilder url = new StringBuilder();
        
        int port = request.getServerPort();
        if (port < 0) {
            port = 80; // Work around java.net.URL bug
        }
        String scheme = request.getScheme();
        url.append(scheme);
        url.append("://");
        url.append(request.getServerName());
        if ((scheme.equals("http") && (port != 80)) || (scheme.equals("https") && (port != 443))) {
            url.append(':');
            url.append(port);
        }
        url.append(request.getContextPath());
        
        return url.toString();
    }

}
